package com.llxz.blog.classify.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llxz.blog.classify.entity.Banner;
import com.llxz.blog.classify.mapper.BannerMapper;
import com.llxz.blog.classify.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {
    @Autowired
    BannerMapper bannerMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Banner> selectBannerList(Long limit) {
        List<Banner> banners =( List<Banner> ) redisTemplate.opsForList().leftPop("banners");
        if(banners==null){
            QueryWrapper<Banner> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("sort");
            wrapper.last("limit " + limit);
            banners = bannerMapper.selectList(wrapper);
            redisTemplate.opsForList().rightPush("banners",banners);
            System.out.println("更新redis-banners:"+ banners);
            redisTemplate.expire("banners",30,TimeUnit.SECONDS);
        }
        System.out.println("输出的bannners：" +banners);
        return banners;
    }
}
